const app=new Vue({
  el:'#app',
  data:{
    books:[
      {
        id:1,
        name:'《计算机导论》',
        date:'2006-9',
        price:85.00,
        count:1
      },
      {
        id:2,
        name:'《UNIX编程艺术》',
        date:'2006-2',
        price:59.00,
        count:1
      },
      {
        id:3,
        name:'《编程珠玑》',
        date:'2008-10',
        price:39.00,
        count:1
      },
      {
        id:4,
        name:'《代码大全》',
        date:'2008-10',
        price:128.00,
        count:1
      }
    ],

  },
  methods:{

    getFinalPrice(price){
      <!-- .toFixed(2) 保留两位小数-->
      return '￥'+price.toFixed(2)
    },
    subCount(i){
        this.books[i].count--;
    },
    addCount(i){
        this.books[i].count++;
    },
    deleteBook(index){
      this.books.splice(index,1);
    },
  },
  computed:{
    allPrice(){
      // 方法一：普通的for循环
      /*let allPrice=0;
      for(let i=0; i< this.books.length; i++){
        allPrice=this.books[i].count*this.books[i].price+allPrice
      }
      return allPrice;*/

      // 方法二：for(let i in this.books)
      /*let allPrice=0;
      for(let i in this.books){ /!* 拿到的i是一个索引值：0 1 2 3 *!/
        allPrice=this.books[i].count*this.books[i].price+allPrice
      }
      return allPrice;*/

      //方法三：for(let item of this.books)
      /*let allPrice=0;
      for(let item of this.books){
        allPrice+=item.price*item.count;
      }
      return allPrice;*/

      //方法四：reduce函数
     return  this.books.reduce(function(preValue,book){
        return preValue+(book.price*book.count)
      },0)
    }
  },
  filters:{ /* 过滤器 */
    showPrice(price){
      return '￥'+price.toFixed(2);
    }

    }
})

